<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/30 0030
 * Time: 上午 10:15
 */
namespace app\api\controller;

use app\common\BaseHelper;

use think\Db;
use think\Exception;
use think\Request;
use \app\admin\model\Joinmeeting;
/*
 * 参会列表
 * */
class Meeting extends BaseController
{

    /*
     * 会议详情
     * */
    public function myMeetingDetail()
    {
        $param = self::getHttpParam();
        if (empty($param->meetingId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'meetingId不能为空');
        }
        if (empty($param->userId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'userId不能为空');
        }
        $where = [
            'id' => $param->meetingId,
            'status'    => 1
        ];
        $fields = "id as meetingId ,title,createOper,startTime,endTime,address,content,signCode,remind";
        $result = \app\admin\model\Meeting::where($where)->field($fields)->find();
        $result['date'] = substr($result['startTime'],0,10);
        $result['startTime'] = substr($result['startTime'],11);
        $result['endTime'] = substr($result['endTime'],11);
        $result['signCode'] = 'https://' . $_SERVER['HTTP_HOST'] . '/public/upload/' . $result['signCode'];

        $name = \app\admin\model\User::field('p_name')->find(['id'=>$result['createOper']]);
        if (empty($name)){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'未查到创建人');
        }
        $result['createOperName'] = $name['p_name'];
        $inMeetingAll = count(\app\admin\model\Joinmeeting::where(['meetingId'=>$param->meetingId])->select());
        $inMeeting    = count(\app\admin\model\Joinmeeting::where(['meetingId'=>$param->meetingId,'isNotarize'=>1])->select());
        $isSign    = count(\app\admin\model\Joinmeeting::where(['meetingId'=>$param->meetingId,'isSign'=>1])->select());
        // 参会总人数
        $result['countAlluser'] = $inMeetingAll;
        // 已经确认参会人数
        $result['countNotarize'] = $inMeeting;
        // 已经签到人数
        $result['countSign'] = $isSign;

        $where = [
            'meetingId' => $param->meetingId,
        ];
        $fields = "memberId as userId";
        $ids = \app\admin\model\Joinmeeting::where($where)->field($fields)->select();

        $member = [];
        foreach($ids as $v)
        {
            $member[] = \app\admin\model\User::alias('a')
                ->where(['meetingId'=>$param->meetingId])
                ->field('a.p_imgUrl,a.p_name,a.id ,a.duties,p.isSign,p.isNotarize')
                ->join('party_meeting_users p','a.id = p.memberId')
                ->find(['a.id'=>$v['userId']]);
        }

        $result['ids'] = $member;

        return show(config('status.SUCCESS_STATUS'),self::MSG_SUCCESS, $result);
    }

    /*
     * 获取确认参会/不参会 列表
     * */
    public function getJoinmeetingList()
    {
        $param = self::getHttpParam();
        if (empty($param->meetingId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'meetingId不能为空');
        }
        if (empty($param->userId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'userId不能为空');
        }
        if (empty($param->type)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'type 不能为空');
        }
        if (!in_array($param->type, [1, 2])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'type 格式不正确');
        }

        $where = [
            'id' => $param->meetingId,
            'status'    => 1
        ];
        $fields = "id as meetingId ,title,createOper,startTime,endTime,address,content";
        $result = \app\admin\model\Meeting::where($where)->field($fields)->find();
        $result['date'] = substr($result['startTime'],0,10);
        $result['startTime'] = substr($result['startTime'],11);
        $result['endTime'] = substr($result['endTime'],11);

        $name = \app\admin\model\User::field('p_name')->find(['id'=>$result['createOper']]);
        if (empty($name)){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'未查到创建人');
        }
        $result['createOper'] = $name['p_name'];

        if($param->type == 1){
            // 确认参会
            $isNotarize = 1;
        }else{
            // 不参会
            $isNotarize = 3;
        }
        $joinWhere = [
            'isNotarize' => $isNotarize,
            'meetingId' => $param->meetingId
        ];
        $fields = "memberId as userId";
        $ids = \app\admin\model\Joinmeeting::where($joinWhere)->field($fields)->select();
        if (empty($ids)){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'未查到确认人');
        }
        $ids = array_column($ids, 'userId');

        $member = \app\admin\model\User::alias('a')
            ->field('a.p_imgUrl,a.p_name,a.id ,a.duties,p.notarizeDate')
            ->join('party_meeting_users p','a.id = p.memberId','left')
            ->where('p.meetingId',$param->meetingId)
            ->whereIn('a.id', $ids)
            ->select();

        if (empty($member)){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'未查到确认人信息');
        }
        $result['ids'] = $member;
        $result['number'] = count($member);

        return show(config('status.SUCCESS_STATUS'),self::MSG_SUCCESS,$result);
    }

    /*
     * 是否确认参会
     * */
    public function isJoinmeeting()
    {
        $param = self::getHttpParam();
        if (empty($param->meetingId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'meetingId不能为空');
        }
        if (empty($param->userId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'userId不能为空');
        }
        if (empty($param->type)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'type不能为空');
        }
        $where = [
            'meetingId'  => $param->meetingId,
            'memberId'   => $param->userId
        ];
        $isjoin =  \app\admin\model\Joinmeeting::where($where)->find();
        if ($isjoin['isNotarize'] == 1){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'已确认过参与该会议');
        }
        if (!$isjoin){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'您无法参加该会议');
        }
        //参会
        $time = date('Y-m-d H:i:s',time());
        if ($param->type == 1){
            $result = \app\admin\model\Joinmeeting::where($where)->update(['isNotarize'=>1,'notarizeDate'=>$time],true);
            if (!$result){
                return show(config('status.ERROR_STATUS'),self::NOT_DATA,'确认参会失败');
            }
            return show(config('status.SUCCESS_STATUS'),self::MSG_SUCCESS,'确认参会成功');
        }
        //不参会
        $result = \app\admin\model\Joinmeeting::where($where)->update(['isNotarize'=>3],true);
        if (!$result){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'操作失败失败');
        }
        return show(config('status.SUCCESS_STATUS'),self::MSG_SUCCESS,'确认不参会');
    }

    /*
     * 会议签到/未签到 列表
     * */
    public function getSignList()
    {
        $param = self::getHttpParam();
        if (empty($param->meetingId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'meetingId不能为空');
        }
        if (empty($param->userId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'userId不能为空');
        }
        if (empty($param->type)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'type 不能为空');
        }
        if (!in_array($param->type, [1, 2])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'type 格式不正确');
        }

        $where = [
            'id' => $param->meetingId,
            'status'    => 1
        ];
        $fields = "id as meetingId ,title,createOper,startTime,endTime,address,content,signCode";
        $result = \app\admin\model\Meeting::where($where)->field($fields)->find();
        $result['date'] = substr($result['startTime'],0,10);
        $result['startTime'] = substr($result['startTime'],11);
        $result['endTime'] = substr($result['endTime'],11);
        $result['signCode'] = 'https://' . $_SERVER['HTTP_HOST'] . '/public/upload/' . $result['signCode'];
        $name = \app\admin\model\User::field('p_name')->find(['id'=>$result['createOper']]);
        if (empty($name)){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'未查到创建人');
        }
        $result['createOper'] = $name['p_name'];
        if($param->type == 1){
            // 签到
            $isSign = 1;
        }else{
            // 未签到
            $isSign = 2;
        }
        $joinWhere = [
            'isSign' => $isSign,
            'meetingId' => $param->meetingId
        ];
        $fields = "memberId as userId";
        $ids = \app\admin\model\Joinmeeting::where($joinWhere)->field($fields)->select();
        if (empty($ids)){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'未查到确认人');
        }
        $member = [];
        foreach($ids as $v)
        {
            $member[] = \app\admin\model\User::alias('a')
                ->field('a.p_imgUrl,a.p_name,a.id ,a.duties,p.signDate')
                ->join('party_meeting_users p','a.id = p.memberId')
                ->find(['a.id'=>$v['userId']]);
            $signDate = \app\admin\model\Joinmeeting::where(['memberId'=>$v['userId'],'meetingId' => $param->meetingId])->find();
            $member[0]['signDate'] = $signDate['signDate'];
        }
        if (empty($member)){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'未查到确认人信息');
        }

        $result['ids'] = $member;
        $result['number'] = count($member);
        return show(config('status.SUCCESS_STATUS'),self::MSG_SUCCESS,$result);
    }

    /*
     * 签到会议
     * */
    public function isSign()
    {
        $param = self::getHttpParam();
        if (empty($param->meetingId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'meetingId不能为空');
        }
        if (empty($param->userId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'userId不能为空');
        }

        $meeting = \app\admin\model\Meeting::where(['id' => $param->meetingId])->find();
        if (!$meeting){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'暂无数据');
        }
        $startTime = strtotime($meeting['startTime']);

        $date = time();
        if ($date > ($startTime + 900) || $date < ($startTime - 900)){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'不在签到时间内');
        }
        //判断是否参加过该会议
        $where = [
            'meetingId'  => $param->meetingId,
            'memberId'   => $param->userId
        ];
        $isjoin =  \app\admin\model\Joinmeeting::where($where)->find();
        if ($isjoin['isNotarize'] != 1){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'还未确认参加该会议');
        }
        if (!$isjoin){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'您无法参加该会议');
        }
        $time = date('Y-m-d H:i:s',time());
        $result = \app\admin\model\Joinmeeting::where($where)->update(['isSign'=>1,'signDate'=>$time]);
        if (!$result){
            return show(config('status.ERROR_STATUS'),self::NOT_DATA,'签到失败');
        }
        return show(config('status.SUCCESS_STATUS'),self::MSG_SUCCESS,'签到成功');
    }

    /*
     * 搜索会议
     * */
    public function searchMeeting()
    {
        $param = self::getHttpParam();
        if (empty($param->userId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'userId不能为空');
        }
        $start = 0;
        $length = 5;
        if (!empty($param->start)){
            $start = $param->start;
        }
        if (!empty($param->length)){
            $length = $param->length;
        }
        //我参与的会议
        $model = new joinmeeting();
        $where = [
            'a.memberId' => $param->userId,
            'm.status'   => 1,
        ];
        if (!empty($param->title)){
            $where['m.title'] = ['like', '%'.$param->title.'%'];
        }
        $fields = "a.memberId as userId, a.isSign , a.isNotarize ,m.id as meetingId, m.startTime, m.endTime ,m.title ,m.address";
        $result = $model->alias('a')
            ->where($where)
            ->field($fields)
            ->join('party_meeting m','a.meetingId = m.id','left')
            ->select();
        foreach($result as $v)
        {
            $v['date'] = substr($v['startTime'],0,10);
            $v['startTime'] = substr($v['startTime'],11);
            $v['endTime'] = substr($v['endTime'],11);
        }
        $data['myJoinMeeting'] = $result;

        //我创建的会议

        $where = [
            'createOper' => $param->userId,
            'status'   => 1,
        ];
        if (!empty($param->title)){
            $where['title'] = ['like', '%'.$param->title.'%'];
        }

        $item = \app\admin\model\Meeting::where($where)->select();

        foreach($item as $v)
        {
            $v['date'] = substr($v['startTime'],0,10);
            $v['startTime'] = substr($v['startTime'],11);
            $v['endTime'] = substr($v['endTime'],11);
        }
        //去重
        $myMeeting = $item;
        foreach($myMeeting as $v)
        {

            foreach ($data['myJoinMeeting'] as $val)
            {
                if ($v['id'] != $val['meetingId']){
                    $myMeeting[] = $val;
                }
            }
        }
        $myMeeting = array($myMeeting);
        $myMeeting = $this->pageMeeting($myMeeting,$start,$length);
        $tatol = count($myMeeting);
        if ($data){
            return showTotal(config('status.SUCCESS_STATUS'),self::MSG_SUCCESS,$myMeeting,$tatol);
        }
        return show(config('status.ERROR_STATUS'),self::NOT_DATA,'暂无数据');
    }

    public function pageMeeting($data,$start,$length)
    {
        $page = [];
        //dump($data[0][1]);die;
        for($i = $start; $i < $length ;$i++)
        {
           if (!empty($data[0][$i])){
               $page[] = $data[0][$i];
           };
        }

        return $page;
    }
}
